package com.xyh.transaction.meilisearch;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.SearchResult;
import com.meilisearch.sdk.model.SearchResultPaginated;
import com.meilisearch.sdk.model.Searchable;
import com.meilisearch.sdk.model.TaskInfo;
import com.xyh.transaction.common.Constant;
import com.xyh.transaction.common.GoodsCondition;
import com.xyh.transaction.entity.vo.GoodsVo;
import com.xyh.transaction.entity.vo.ImageVo;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

public class SearchTest {


    public static void main(String[] args) throws MeilisearchException {
        SearchTest searchTest = new SearchTest();

        searchTest.searchList();
    }

    public void searchList() throws MeilisearchException {

//        Client client = new Client(new Config("http://localhost:7700", "aSampleMasterKey"));

        Client client = new Client(new Config("http://124.223.56.87:7700", "aSampleMasterKey"));

        List<GoodsVo> goodsVos = client.index("goods").search("游戏").getHits().stream().map(map -> {
            GoodsVo goodsVo = new GoodsVo();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
            goodsVo.setUpdateTime(LocalDateTime.parse(map.get("updateTime").toString(), formatter));
            goodsVo.setCategoryName(map.get("categoryName").toString());
            goodsVo.setPicture(map.get("picture").toString());

            ArrayList<Map<String,Object>> data = (ArrayList<Map<String,Object>>) map.get("pictures");
            List<ImageVo> pictures = data.stream().map(p -> {
                ImageVo imageVo = new ImageVo();
                imageVo.setName(p.get("name").toString());
                imageVo.setUrl(p.get("url").toString());
                return imageVo;
            }).collect(Collectors.toList());
            goodsVo.setPictures(pictures);
            goodsVo.setCondition(GoodsCondition.valueOf(map.get("condition").toString()));
            goodsVo.setCreateBy(Double.valueOf(map.get("createBy").toString()).intValue());
            goodsVo.setCreateTime(LocalDateTime.parse(map.get("createTime").toString()));
            goodsVo.setUpdateBy(Double.valueOf(map.get("updateBy").toString()).intValue());
            goodsVo.setPrice(new BigDecimal(map.get("price").toString()));
            goodsVo.setName(map.get("name").toString());
            goodsVo.setId(Double.valueOf(map.get("id").toString()).intValue());
            goodsVo.setStock(Double.valueOf(map.get("stock").toString()).intValue());
            goodsVo.setCategoryId(Double.valueOf(map.get("categoryId").toString()).intValue());
            goodsVo.setDesc(map.get("desc").toString());

            return goodsVo;
        }).collect(Collectors.toList());



//        System.out.println(client.index("goods").search("游戏").getHits());
//        for (Object o : JSONObject.parseArray(String.valueOf(client.index("goods").search("游戏").getHits()), new Class[]{GoodsVo.class})) {
//            System.out.println(o);
//        }
//        SearchResult searchResult = client.index("movies").search("\"african american\" horror");

//        SearchRequest searchRequest = SearchRequest.builder()
//                .q("shifu") // 查询参数
//                .offset(1) // 跳过第一条
////                .limit(2) // 查询仅返回两个文档
//                .hitsPerPage(15) // 返回查询的前 15 个结果
//                .build();
//        Searchable searchResult = client.index("movies").search(searchRequest);

//        SearchRequest searchRequest = SearchRequest.builder().q("").page(15).build(); // 返回搜索结果的第二页：
//        SearchResultPaginated searchResult = (SearchResultPaginated) client.index("movies").search(searchRequest);

//        SearchRequest searchRequest =
//                SearchRequest.builder().q("thriller").filterArray(new String[][] {
//                        new String[] {"genres = Horror", "genres = Mystery"}}).build();
//
//        Searchable searchResult = client.index("movies").search(searchRequest);

//        // 要仅获取 overview 和 title 字段，请将 attributesToRetrieve 设置为 ["overview", "title"] 。
//        SearchRequest searchRequest = SearchRequest.builder().q("a").attributesToRetrieve(new String[] {"overview", "title"}).build();
//        Searchable searchResult = client.index("movies").search(searchRequest);

//        SearchRequest searchRequest = SearchRequest.builder().q("adventure").attributesToSearchOn(new String[] {"overview"}).build();
//
//        Searchable searchResult = client.index("movies").search(searchRequest);
//
//
//        System.out.println(searchResult);


    }

}
